﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter21.Code02.Example06
{
    using System.IO;

    public class Demo
    {
        public static void Begin()
        {
            try
            {
                string filename = "compressdFile.txt";
                Console.WriteLine("Enter a string to compress (will be repeated 100 times):");
                string sourceString = Console.ReadLine();
                StringBuilder sourceStringMultiplier = new StringBuilder(sourceString.Length * 100);
                for (int i = 0; i < 100; i++)
                {
                    sourceStringMultiplier.Append(sourceString);
                }
                sourceString = sourceStringMultiplier.ToString();
                Console.WriteLine("Source data is {0} bytes long.", sourceString.Length);
                Compressor.SaveCompressedFile(filename, sourceString);
                Console.WriteLine("\nData saved to {0}.", filename);
                FileInfo compressedFileData = new FileInfo(filename);
                Console.WriteLine("Compressed file is {0} bytes long.", compressedFileData.Length);
                string recoveredString = Compressor.LoadCompressedFile(filename);
                recoveredString = recoveredString.Substring(0, recoveredString.Length / 100);
                Console.WriteLine("\nRecovered data: {0}", recoveredString);
                Console.ReadLine();
            }
            catch (IOException e)
            {
                Console.WriteLine("An IO exception has heen thrown!");
                Console.WriteLine(e.ToString());
                Console.ReadLine();
            }
        }
    }
}
